<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/5/7
 * Time: 10:55
 * 好友列表
 */
namespace app\api\controller;
use think\Db;
class Friend extends Base {

    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {

        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }
    }
    
    //统计好友的数量
    public function countNum(){
        extract(input());
        $where['friend_status'] = 1;
        $where['friend_user_id'] = $user_id;
        $list = Db::table('up_friend')->where($where)->count();
        return  json(array('status'=>200,'msg'=>$list));
    }


    //好友列表
    public function friendList(){
        extract(input());
        $where1['friend_status'] = 1;
        $where1['friend_user_id'] = $user_id;
        $count = Db::table('up_friend')->where($where1)->count();
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        $where['a.friend_status'] = 1;
        $where['a.friend_user_id'] = $user_id;
        $list = Db::table('up_friend')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.friend_other_user','left')
            ->join('up_exp_level uel','uel.el_id = uuu.user_level','left')
            ->field('uuu.user_gender,a.friend_id,uuu.user_username,uel.el_name,uuu.user_age,uuu.user_image')
            ->order('a.friend_time desc')
            ->where($where)->limit($limit,$num)->select();
        $count_wait = Db::table('up_friend')
            ->where('friend_other_user',$user_id)
            ->where('friend_status',2)
            ->count();

        return  json(array('status'=>200,'msg'=>$list,'num'=>$count,'count_wait'=>$count_wait));
    }

    //删除好友 拉黑好友  从黑名单移除好友
    public function delFriend(){
        extract(input());
        $friend_id = input('param.friend_id');
        $type =input('param.type');//1删除好友 2拉黑  3从黑名单移除好友
        if($type==1){//删除好友
            $info = Db::table('up_friend')->where('friend_id',$friend_id)->find();
            $where['friend_user_id'] = $info['friend_other_user'];
            $where['friend_other_user'] = $info['friend_user_id'];
            $res = Db::table('up_friend')->where($where)->delete();
            $result = Db::table('up_friend')->where('friend_id',$friend_id)->delete();
            if($res&&$result){
                return  json(array('status'=>200,'msg'=>"成功"));
            }else{
                return  json(array('status'=>100,'msg'=>"失败"));
            }
        }elseif ($type==2){//拉黑
            $res = Db::table('up_friend')->where('friend_id',$friend_id)->setField('friend_status',4);
            if($res){
                return  json(array('status'=>200,'msg'=>"成功"));
            }else{
                return  json(array('status'=>100,'msg'=>"失败"));
            }
        }elseif ($type==3){
            $res = Db::table('up_friend')->where('friend_id',$friend_id)->setField('friend_status',1);
            if($res){
                return  json(array('status'=>200,'msg'=>"成功"));
            }else{
                return  json(array('status'=>100,'msg'=>"失败"));
            }
        }
    }

    //黑名单
    public function blackFriend(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        $where['a.friend_status'] = 4;
        $where['a.friend_user_id'] = $user_id;
        $list = Db::table('up_friend')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.friend_other_user','left')
            ->join('up_exp_level uel','uel.el_id = uuu.user_level','left')
            ->field('uuu.user_gender,a.friend_id,uuu.user_username,uel.el_name,uuu.user_age,uuu.user_image')
            ->where($where)->limit($limit,$num)->select();
        return  json(array('status'=>200,'msg'=>$list));
    }

    //新好友
    public function newFriend(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        $where['a.friend_status'] = array('IN',array(1,2,3));
        $where['a.friend_other_user'] = $user_id;
        $list = Db::table('up_friend')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.friend_user_id','left')
            ->join('up_grade ug','ug.grade_id = uuu.user_grade','left')
            ->join('up_class uc','uc.class_id=uuu.user_class','left')
            ->join('up_school us','us.school_id = uuu.user_school','left')
            ->field('us.school_name,ug.grade_name,uc.class_name,a.friend_status,uuu.user_gender,a.friend_id,uuu.user_username,uuu.user_age,uuu.user_image')
            ->where($where)
            ->limit($limit,$num)
            ->order('friend_time desc')
            ->select();
        return  json(array('status'=>200,'msg'=>$list));
    }

    //同意好友  拒绝好友
    public function addFriend(){
        extract(input());
        $friend_id = input('param.friend_id');
        $type = input('param.type');//1同意  2拒绝
//        $friend_id = 8;
//        $type = 1;
        //查询数据
        $info = Db::table('up_friend')->where('friend_id',$friend_id)->find();
        if($type==1){//同意
            $where['friend_user_id'] = $info['friend_other_user'];
            $where['friend_other_user'] =  $info['friend_user_id'];
            //
            $res = Db::table('up_friend')->where($where)->find();
            if($res['friend_id']!=''){//证明已经存在数据了
                $data1['friend_status'] = 1;
                $data1['friend_time'] = date('Y-m-d H:i:s',time());
                $res1 = Db::table('up_friend')->where('friend_id',$res['friend_id'])->update($data1);
                $res2 = Db::table('up_friend')->where('friend_id',$friend_id)->update($data1);
            }else{//没有数据
                $data2['friend_user_id'] = $info['friend_other_user'];
                $data2['friend_other_user'] = $info['friend_user_id'];
                $data2['friend_status'] = 1;
                $data2['friend_time'] = date('Y-m-d H:i:s',time());
                 Db::table('up_friend')->where('friend_id',$friend_id)->setField('friend_status',1);
                $res1 = Db::table('up_friend')->insertGetId($data2);
            }
            if($res1){
                return  json(array('status'=>200,'msg'=>"成功"));
            }else{
                return  json(array('status'=>100,'msg'=>"失败"));
            }
        }elseif ($type==2){//拒绝
            $res1 = Db::table('up_friend')->where('friend_id',$friend_id)->setField('friend_status',3);
            if($res1){
                return  json(array('status'=>200,'msg'=>"成功"));
            }else{
                return  json(array('status'=>100,'msg'=>"失败"));
            }
        }
    }

    //搜索好友
    public function selectFriend(){
        extract(input());
        $list = array();
        $keyword = input('param.keyword');
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        $where = array();
        if($keyword!=''){
            $where['user_username|user_phone'] = $keyword;
        }else{
            return  json(array('status'=>200,'msg'=>$list));
        }
        $list = Db::table('up_u_user')->alias('a')
            ->join('up_grade ug','ug.grade_id = a.user_grade','left')
            ->join('up_class uc','uc.class_id = a.user_class','left')
            ->join('up_school us','us.school_id = a.user_school','left')
            ->field('us.school_name,ug.grade_name,uc.class_name,a.user_id,a.user_gender,a.user_username,a.user_age,a.user_image')
            ->where($where)->limit($limit,$num)->select();
        foreach ($list as $key=>&$vo){
            $map['friend_user_id'] = $user_id;
            $map['friend_other_user'] = $vo['user_id'];
            //查询本人是否已经添加搜索的这个好友了
            $info = Db::table('up_friend')->where($map)->find();
            if($info['friend_id']==''){//没有添加这个人好友
               $vo['status'] = 1;//添加好友
            }else{
                if($info['friend_status']==1){//已经是好友
                  $vo['status'] =2;//已添加好友
                }elseif ($info['friend_status']==3){//已拒绝
                    $vo['status'] = 1;//添加好友
                }elseif ($info['friend_status']==4){//拉黑
                    $vo['status'] = 2;//已添加好友
                }
            }
        }
        return  json(array('status'=>200,'msg'=>$list));
    }


    //添加好友
    public function addToFriend(){
        extract(input());
        $userId = input('param.otherUserId');
        $where['friend_user_id'] = $user_id;//自己
        $where['friend_other_user'] = $userId;//添加的人
        if($user_id==$userId){
            return  json(array('status'=>100,'msg'=>"不能自己添加自己为好友"));
        }
        //未认证教师需要认证
        $seesee = $this->getUserStatusForSee($user_id);
        if($seesee['status'] != 200){
            return json($seesee);
        }
        $info = Db::table('up_friend')->where($where)->find();
        if($info['friend_id']!=''){//证明数据已经存在
            $data['friend_status'] = 2;
            $data['friend_time'] = date('Y-m-d H:i:s',time());
            $map['friend_id'] = $info['friend_id'];
            $res = Db::table('up_friend')->where($map)->update($data);
        }else{//数据没有直接插入
            $data1['friend_user_id'] = $user_id;
            $data1['friend_other_user'] = $userId;
            $data1['friend_status'] = 2;
            $data1['friend_time'] = date('Y-m-d H:i:s',time());
            $res = Db::table('up_friend')->insertGetId($data1);
        }
        if($res){
            $user_name = Db::table('up_u_user')->where('user_id', $user_id)->value('user_username');
            if(mb_strlen($user_name, 'utf-8') > 14){
                $user_name = mb_substr($user_name, 0, 14) . '...';
            }
            $str = '喵喵阅读:' . $user_name . '申请您为好友';
            $user_to_id = (string)$userId;
            try{
                $this->Jpush(1, $user_to_id, $str, ['send_type' => 1]);
            }catch(\Exception $e){
                
            }
            return  json(array('status'=>200,'msg'=>"成功"));
        }else{
            return  json(array('status'=>100,'msg'=>"失败"));
        }
    }
}